<?php
/**
 * Created by PhpStorm.
 * User: 陈森
 * Date: 2020/12/21
 * Time: 13:22
 */

namespace app\api\controller;

//Controller
/**
 * json  0失败  1成功  2跳转  3弹窗   4未激活  5冻结
 */

use app\admin\model\member\BuyModel;
use core\basic\Controller;


class BuyController extends Controller
{
    protected $model;

    public function __construct()
    {


        $this->model = new  BuyModel();
    }

    public function addBuy()
    {



        $quantity = request('quantity', 'num', true, '购买数量');
        $file = request('file', 'url', true, '打款截图');
        $bank_id = request('bank_id', 'null', true, '付款账号');
        $memcacheInfo = $this->Memcache(2, session('uid'));
        $userInfo = model('admin.member.Member')->checkMember(['id' => $memcacheInfo->id]);
        if($this->model->BuyInfo(['user_id'=>$userInfo->id,'status'=>1])){
			json(0, '后台审核中..');
		}
        if ($userInfo->gid == 1) {
            json(0, '账号暂时未激活,无法购买！');
        }
        if($quantity < 10000){
			json(0, '购买金额不能少于10000');
		}
        $paytype = post('payType');
        $data = array(
            'user_id' => $userInfo->id,
            'bank_id' => $bank_id,
            'quantity' => $quantity,
            'paytype' => $paytype,
            'file' => $file,
            'status' => 1,
            'audit_time' => '',
            'add_time' => get_datetime(),
        );
        if (!!$field = $this->model->addBuy($data)) {
			$UserPLog = $userInfo->usermobile.'申请购买' . $quantity . '充值积分' ;
			model('admin.member.Bill')->AddBill($userInfo->id, 1, $UserPLog, 1);
            json(1, '申请成功,请等待审核！');
        } else {
            json(0, '申请失败！');
        }

    }

    // 文件上传方法
    public function upload()
    {

        $qiniu_open = $this->config('qiniu_open');
        $qiniu_access_key = $this->config('qiniu_access_key');
        $qiniu_secret_key = $this->config('qiniu_secret_key');
        $qiniu_bucket = $this->config('qiniu_bucket');
        $qiniu_url = $this->config('qiniu_url');
        if ($qiniu_open == 1 && !empty($qiniu_access_key) && !empty($qiniu_secret_key) && !empty($qiniu_bucket) && !empty($qiniu_url)) {
            $upload = qiniu_upload('upload', null, null, null, $qiniu_access_key, $qiniu_secret_key, $qiniu_bucket, $qiniu_url);
        } else {
            $upload = upload('upload');
        }
        if (is_array($upload)) {
            json(1, $upload[0]);
        } else {
            json(0, $upload);
        }

    }
}